<?php
/**
 * 后台图片素材管理
 * @package app\admin\controller
 * User: Administrator
 * Date: 2018/10/26 0013
 * Time: 14:55
 */
namespace app\admin\controller;

use service\DataService;
use service\ToolsService;
use think\Db;
class Thumb extends BasicAdmin
{

    public $table = 'ml_thumb';

    /**
     *  图片列表
     */
    public function index()
    {
        $this->title = '图片管理 > 图片列表';
        $params = [];
        $db = \app\admin\model\Thumb::where(1);
        if($this->request->isPost()){
            $param = array_filter($this->request->param());
            foreach (['title'] as $key) {
                (isset($param[$key]) && $param[$key] !== '') && $db->whereLike('id', "%{$param[$key]}%");
            }
            if(isset($param['nickname']) && $param['nickname']){
                $member_id = \app\admin\model\Member::whereLike('nickname',"%{$param['nickname']}%")->column('id');
                $db->where('member_id','in',$member_id);
            }
          /*  if(isset($param['tagids']) && $param['tagids']){
                $tag_id = \app\admin\model\Tag::whereLike('tag',"%{$param['tagids']}%")->column('id');
                if($tag_id){
                    foreach ($tag_id as $v) {
                        $db->whereOr("tagids","like","%,{$v},%");
                    }
                }
            }*/
            if(isset($param['catid']) && $param['catid']) {
                $pid = \app\admin\model\Category::where('id', $param['catid'])->value('pid');
                $pid > 0 ? $db->where('catid', $param['catid']) : $db->where('category', $param['catid']);
            }
            (isset($param['status']) && $param['status'] !== '') && $db->where('status',$param['status']);
            (isset($param['is_free']) && $param['is_free'] !== '') && $db->where('is_free',$param['is_free']);
            $db->order('status desc, id '.$param['order']);
        }
        return  parent::_list($db,$params);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        if($this->request->isPost()) {
            $froms = array_unique(array_column($data, 'member_id'));
            $list = \app\admin\model\Member::where('id', 'in', $froms)->column('id,nickname');
            //分类
            $catids = array_unique(array_merge(array_column($data, 'category'), array_column($data, 'catid')));
            $cate_list = \app\admin\model\Category::where('id', 'in', $catids)->column('id,cate');
            foreach ($data as &$vo) {
                $vo['nickname'] = $vo['member_id'] > 0 ? $list[$vo['member_id']] : '';
                $vo['channel'] = $vo['channel'] == 1 ? '原创' : $vo['from'];
                $vo['category'] = $vo['category'] ? $cate_list[$vo['category']] : '';
                $vo['catid'] = $vo['catid'] ? $cate_list[$vo['catid']] : '';
                $vo['thumb_20'] = $vo['thumb']?ToolsService::thumb($vo['thumb'],20,20,'w',2):'';
                $vo['thumb_750'] = $vo['thumb']?ToolsService::thumb($vo['thumb'],1200,750,'w',2):'';

            }
        }
        if($this->request->isGet()){
            //获取分类
            $_cates = Db::name('ml_cate')->order('sort desc,id desc')->select();
            $cates = ToolsService::arr2table($_cates);
            $this->assign('cates', $cates);
            //获取所有用户信息
            //$users = \app\admin\model\Member::where('status',1)->column('id,rename');
            //$this->assign('users', $users);
            //get传值参数
            if($params = array_filter($this->request->param())){
                foreach($params as $k => $v){
                    !in_array($k,['limit','page','order']) && $this->assign('get_'.$k, $v);
                }
            }
            $this->assign('status_arr',config('thumb.status'));
        }
    }

    /**
     * 编辑图片(审核，上下架操作)
     */
    public function edit()
    {
        if($this->request->isPost()){
            $rst = DataService::update($this->table);
            $msg = doReturn('更新成功','更新失败',$rst);
            $this->resultArr($msg);
        }
    }

    /**
     * 查看详情
     */
    public function info()
    {
        return $this->_form($this->table, 'info');
    }


    /**
     * 图片说明页面
     */
    public function content(){
        $id = $this->request->param('id','','trim');
        $content = \app\admin\model\Thumb::where('id',$id)->value('content');
        return $this->fetch('',['content'=>$content]);
    }

    /**
     * 图片审核页面
     */
    public function status(){
        if($this->request->isPost()){
            $data = array_filter($this->request->post());
            $rst = false;
            if(false !== \app\admin\model\Thumb::update($data)) $rst = true;
            $msg = doReturn('更新成功','更新失败',$rst);
            $this->resultArr($msg);
        }
        $id = $this->request->param('id','','trim');
        $iframe = $this->request->param('iframe','','trim');
        $vo = \app\admin\model\Thumb::find($id);
        return $this->fetch('',['vo'=>$vo,'iframe'=>$iframe]);
    }

    /**
     * 删除图片
     */
    public function del()
    {
        $status = DataService::update($this->table);
        $ret = doReturn('图片删除成功！','图片删除失败, 请稍候再试!',$status,[],'json');
        $this->resultArr($ret);
    }

    protected function _form_filter(&$data){
        //会员
        $data['member'] = \app\admin\model\Member::where('id',$data['member_id'])->value('nickname');
        $data['thumb_430'] =  $data['thumb_20'] = $data['thumb']?ToolsService::thumb($data['thumb'],430,320,'h',2):'';
        //类目
        $data['category'] = \app\admin\model\Category::where('id',$data['category'])->value('cate');
        $data['catid'] = \app\admin\model\Category::where('id',$data['catid'])->value('cate');
        //所属标签
        $data['tagids'] = array_filter(explode(',',$data['tagids']));
        $data['tags'] = \app\admin\model\Tag::where('id','in',$data['tagids'])->column('tag');
        foreach(['type','quality','channel','is_free','show'] as $v){
            $data[$v] = config('thumb.'.$v)[$data[$v]];
        }
    }
}
